#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 发送GET请求
//    sendTcpRequest();

    // 发送POST请求
        sendPostRequest();
}
void MainWindow::sendTcpRequest()
{
    socket=new QTcpSocket(this);
    socket->connectToHost("localhost", 8080);
    connect(socket ,&QTcpSocket::readyRead,this,&MainWindow::read_date);
    if (socket->waitForConnected())
    {
        // 发送TCP请求
        // 构造请求的JSON数据
        QJsonObject requestData;
        QJsonObject polyObject;
        QJsonObject geometryObject;
        QJsonArray coordinatesArray;

        coordinatesArray.append(QJsonArray() << 108.90682388195336 << 34.24676569488925);
        coordinatesArray.append(QJsonArray() << 108.90682388195336 << 34.24633009762056);
        coordinatesArray.append(QJsonArray() << 108.90730770962608 << 34.24633009762056);
        coordinatesArray.append(QJsonArray() << 108.90730770962608 << 34.24676569488925);

        //        QJsonArray landing_point;
        //        landing_point.append(108.905507);
        //        landing_point.append(34.248668);

        geometryObject["type"] = "Polygon";
        geometryObject["coordinates"] = coordinatesArray;

        polyObject["type"] = "Feature";
        polyObject["properties"] = QJsonObject();
        polyObject["geometry"] = geometryObject;

        requestData["poly"] = polyObject;
        requestData["landing_point"]=QJsonArray() <<108.905507 <<34.248668;
        requestData["height"] = 100.0;
        requestData["speed"] = 10;
        requestData["angle"] = 90;
        requestData["overlap"] = QJsonArray() << 85 << 80;
        QJsonDocument jsonDoc(requestData);
        qDebug() << "request is: ";
        qDebug().noquote() << QJsonDocument(requestData).toJson(QJsonDocument::Indented);
        qint64 bytesWritten = socket->write(jsonDoc.toJson());
        if (bytesWritten == -1)
        {
            qDebug() << "Failed to write data:" << socket->errorString();
            return ;
        }

    }
}
void MainWindow::sendPostRequest()
{

    // 构造请求的JSON数据
    QJsonObject requestData;
    QJsonObject polyObject;
    QJsonObject geometryObject;
    QJsonArray coordinatesArray;

    coordinatesArray.append(QJsonArray() << 108.90682388195336 << 34.24676569488925);
    coordinatesArray.append(QJsonArray() << 108.90682388195336 << 34.24633009762056);
    coordinatesArray.append(QJsonArray() << 108.90730770962608 << 34.24633009762056);
    coordinatesArray.append(QJsonArray() << 108.90730770962608 << 34.24676569488925);

    //        QJsonArray landing_point;
    //        landing_point.append(108.905507);
    //        landing_point.append(34.248668);

    geometryObject["type"] = "Polygon";
    geometryObject["coordinates"] = coordinatesArray;

    polyObject["type"] = "Feature";
    polyObject["properties"] = QJsonObject();
    polyObject["geometry"] = geometryObject;

    requestData["poly"] = polyObject;
    requestData["landing_point"]=QJsonArray() <<108.905507 <<34.248668;
    requestData["height"] = 100.0;
    requestData["speed"] = 10;
    requestData["angle"] = 90;
    requestData["overlap"] = QJsonArray() << 85 << 80;

    // 发送POST请求
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    connect(manager, &QNetworkAccessManager::finished, this, &MainWindow::onReplyFinished);

    QNetworkRequest request(QUrl("http://localhost:8080/planRoute"));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");

    QJsonDocument jsonDoc(requestData);
    manager->post(request, jsonDoc.toJson());

}
void MainWindow::onReplyFinished(QNetworkReply *reply)
{
    if (reply->error() == QNetworkReply::NoError)
    {
        QByteArray responseData = reply->readAll();
        QJsonDocument jsonResponse = QJsonDocument::fromJson(responseData);

        // 处理响应数据
        QJsonObject jsonObject = jsonResponse.object();
        qDebug() << "Response:";
        qDebug().noquote() << QJsonDocument(jsonObject).toJson(QJsonDocument::Indented);
    }
    else
    {
        qDebug() << "Error:" << reply->errorString();
    }

    reply->deleteLater();
}
void MainWindow::read_date()
{
    QJsonDocument jsonResponse = QJsonDocument::fromJson(socket->readAll());
    //        QJsonDocument jsonResponse(rade_date);

    // 处理响应数据
    //        QJsonObject jsonObject = jsonResponse.object();
    qDebug() << "Response is:";
    qDebug().noquote() << QJsonDocument(jsonResponse).toJson(QJsonDocument::Indented);
    //        socket.disconnectFromHost();
    //        socket.waitForDisconnected();
}
MainWindow::~MainWindow()
{
    delete ui;
}
